package com.lqgao.controller;

import com.lqgao.entity.*;
import com.lqgao.service.AttractionService;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * (Attraction)表控制层
 *
 * @author makejava
 * @since 2023-06-20 23:31:43
 */
@RestController
@RequestMapping("/api/attraction")
public class AttractionController {
    /**
     * 服务对象
     */

    private final AttractionService attractionService;

    public AttractionController(AttractionService attractionService) {
        this.attractionService = attractionService;
    }

    /**
     * 分页查询
     *
     * @param attraction  筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping("/page")
    public Result<PageResult<Attraction>> queryByPage(Attraction attraction, PageRequest pageRequest) {
        PageResult<Attraction> attractionPageResult = attractionService.queryByPage(attraction, pageRequest);
        return Result.<PageResult<Attraction>>builder()
                .code(200)
                .msg("操作成功")
                .data(attractionPageResult)
                .build();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result<Attraction> queryById(@PathVariable("id") Integer id) {
        Attraction attraction = attractionService.queryById(id);
        return Result.<Attraction>builder()
                .code(200)
                .msg("success")
                .data(attraction)
                .build();
    }

    /**
     * 新增数据
     *
     * @param attraction 实体
     * @return 新增结果
     */
    @PostMapping
    public Result<Attraction> add(@RequestBody Attraction attraction) {
        Attraction insert = attractionService.insert(attraction);
        return Result.<Attraction>builder()
                .code(200)
                .msg("添加成功")
                .data(insert).build();
    }

    /**
     * 编辑数据
     *
     * @param attraction 实体
     * @return 编辑结果
     */
    @PutMapping
    public Result<Attraction> edit(@RequestBody Attraction attraction) {
        Attraction update = attractionService.update(attraction);
        return Result.<Attraction>builder()
                .code(200)
                .msg("添加成功")
                .data(update)
                .build();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public Result<Boolean> deleteById(Integer id) {
        attractionService.deleteById(id);
        return Result.<Boolean>builder()
                .code(200)
                .msg("删除成功")
                .build();
    }

    @GetMapping("/getMainData")
    public Result<List<ChartData>> getMainData() {
        List<ChartData> chartDataList = attractionService.getMainData();
        return Result.<List<ChartData>>builder()
                .code(200)
                .msg("success")
                .data(chartDataList)
                .build();
    }

    @GetMapping("/getSubData")
    public Result<List<ChartData>> getSubData() {
        List<ChartData> chartDataList = attractionService.getSubData();
        return Result.<List<ChartData>>builder()
                .code(200)
                .msg("success")
                .data(chartDataList)
                .build();
    }

    @GetMapping("/getBasicData")
    public Result<List<ChartData>> getBasicData() {
        List<ChartData> chartDataList = attractionService.getBasicData();
        return Result.<List<ChartData>>builder()
                .code(200)
                .msg("success")
                .data(chartDataList)
                .build();
    }
}

